package com.lecture.junit.tdd.example;

import java.util.concurrent.atomic.AtomicInteger;

public abstract class LoginServiceState {
	static final int MAX_ATTEMPT_COUNT = 3;
	
	final AtomicInteger attemptCounter;
	
	public LoginServiceState(AtomicInteger counter) {
		attemptCounter = counter;
	}

    public void login(LoginServiceInterface context, IAccount acc, String password) throws AccountException {
        if (acc.passwordMatches(password)) {
            
            if (acc.isUsingExpired())
            	acc.setRevoked();
            
        	if (acc.isRevoked())
    			throw new AccountRevokedException();
    		if (acc.isLoggedIn())
    			throw new AccountAlreadyLoggedInException();
    		
    		acc.setLoggedIn();
    		context.setState(acc.getUserName(), new AwaitLoginState(new AtomicInteger(0)));
        }
        else {
	        handlePasswordFail(context, acc);
        }
    }

    protected abstract void handlePasswordFail(LoginServiceInterface context, IAccount acc);
}
